/**
 * 旅游攻略路由
 */

const express = require('express');
const router = express.Router();
const {  readGuide, addGuide, queryGuide, updateGuide, getGuideDetail, updateGuideSomeKey } = require("../file/travelGuide");
const { networkStatus } = require('../utils/constant');


/**
 * 添加旅游攻略
 * @param {*} data 
 * @returns 
 */
router.post('/add', async (req, res) => {
    console.log('body', req.body);
    try {
        await addGuide(req.body);
        res.send({
            code: 200,
            data: null,
            msg: networkStatus['200'],
        })
    }catch (error) {
        console.log(error);
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

/**
 * 查询所有旅游攻略
 * @param {*} data 
 * @returns 
 */
router.post('/findAll', async (req, res) => {
    console.log('body', req.body);
    try {
        const data =  await readGuide(req.body);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    }catch (error) {
        console.log(error);
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})


// 查询旅游攻略
router.post('/query', async (req, res) => {
    try {
        const data = await queryGuide(req.body);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

// 旅游攻略批量修改值
router.post('/mostUpdate', async (req, res) => {
    console.log('body', req.body);
    try {
        const data = await updateGuide(req.body.key);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

// 根据id查询旅游攻略
router.post('/getDetail', async (req, res) => {
    try {
        const data = await getGuideDetail(req.body.id);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

// 修改部分字段
router.post('/updateGuideSomeKey', async (req, res) => {
    const { id, title, address, content } = req.body;
    const info = { title, address, content };
    try {
        const data = await updateGuideSomeKey(id, info);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

/**
 * 删除攻略
 * @param {*} id id
 * @returns 
 */
async function deleteGuide(id) {
    try {
        const res = await readUser();
        const index = res.findIndex((item) => query.id === item.id);
        console.log(index);
        res.splice(index, 1);
        const data = JSON.stringify(res)
        await fs.writeFileSync(`./data/use.txt`, data);
        return Promise.resolve(index);
    } catch (error) {
        return Promise.reject();
    }
}

module.exports = router;
